<template>
    <!-- 注册时间 当前完成案件总数 姓名 导师 来源 申请时间 导师是否通过 -->
    <div>
        <div>
            <table-search
                ref="table-search"
                :selectHead="selectHead"
                @searchMethods="searchMethods"
                :nameOptions="nameOptions">
            </table-search>
        </div>
        <el-table
            @sort-change='tableSortChange'
            :data="tableData"
            border
        >
            <el-table-column
                label="ID"
                width="60"
                align="center">
                <template slot-scope="scope">
                    {{(scope.$index + 1) + (current_page - 1) * per_page}}
                </template>
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="申请人"
                show-overflow-tooltip
                prop="name">
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="完成案件数(件)"
                prop="agent_finals_count"
                align="center"
                show-overflow-tooltip
                width="150">
            </el-table-column>
            <el-table-column
                label="申请时间"
                sortable="custom"
                prop="apply_time"
                align="center"
                show-overflow-tooltip
                min-width="160">
                <!-- <template slot-scope="scope">
                    {{scope.row.userRecord[scope.row.userRecord.findIndex(v => v.type == 2)].created_at}}
                </template> -->
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="注册时间"
                align="center"
                prop="register_time"
                show-overflow-tooltip
                min-width="160">
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                width="150">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" @click="applyOperate(scope.row, 'yes')">通过</el-button>
                    <el-button type="danger" size="small" @click="applyOperate(scope.row, 'no')">拒绝</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination @paginationChange="pageChange" :total="total"></pagination>
    </div>
</template>

<script>
import pagination from '~/components/pagination'
import { getMyInfo } from "~/api/user";
import tableSearch from '~/components/tableSearch'
export default {
    components: {
        pagination,
        tableSearch
    },

    data() {
        return {
            tableData: [],
            current_page: 1,
            per_page: 25,
            total: 0,
            selectHead: [
                {
                    label: '申请人',
                    props: 'name'
                },
                {
                    label: '完成案件数',
                    props: 'agent_finals_count'
                },
                {
                    label: '申请时间',
                    props: 'apply_time'
                },
                {
                    label: '注册时间',
                    props: 'created_at'
                }
            ],
            search_params: {},
            sort_params: {},
            nameOptions: []
        }
    },

    created() {
        this.getTableData()
    },

    methods: {
        tableSortChange(column) {
            this.sort_params = {
                sort_str: column.prop
            }
            if (column.order == 'ascending') {
                this.sort_params.sort_order = 'asc'
            } else if (column.order == 'descending') {
                this.sort_params.sort_order = 'desc'
            } else {
                this.sort_params = {}
            }
            this.getTableData()
        },
        searchMethods(params, no_search) {
            this.search_params = params
            if (!no_search) {
                this.getTableData()
            }
        },
        async getMyinfo () {
            let myInfo = await getMyInfo(this.$axios);
            if (myInfo.data.status == 'success') {
                this.$store.commit("setRoleInfo",myInfo.data.data)
                this.$store.commit("setUser",myInfo.data.data);
            }
        },
        applyOperate(data, type) {
            let msg = `确认${type == 'yes' ? '通过' : '拒绝'}<span class="finish">${data.name}</span>的出师申请吗？`
            this.$confirm(msg, '提示', {
                type: 'warning',
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                center: true,
                dangerouslyUseHTMLString: true
            }).then(() => {
                let params = {
                    agent_id: data.id,
                    pass: type
                }
                this.$axios.post('/api/agent/teacher_graduation_audit', params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: `${type == 'yes' ? '通过' : '拒绝'}出师申请成功`
                        })
                        this.current_page = 1
                        this.getMyinfo()
                        this.getTableData()
                    } else {
                        this.$message({
                            type: 'error',
                            message:　res.data.message
                        })
                    }
                })
            }).catch(() => {})
        },
        pageChange(data) {
            this.per_page = data.perPage
            this.current_page = data.page
            this.getTableData()
        },
        getTableData() {
            let params = {
                page: this.current_page,
                perPage: this.per_page
            }
            params = {...params, ...this.search_params, ...this.sort_params}
            this.$axios.get('/api/agent/graduation_list', {
                params: params
            }).then(res => {
                if (res.data.code == 200) {
                    this.tableData = res.data.data
                    this.nameOptions = res.data.allName
                    this.total = res.data.page.total
                } else {
                    this.$message({
                        type: 'error',
                        message: res.data.message
                    })
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>

</style>
